/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Proxy;

import GateWars.GateWarsFactory;
import Utilities.ArrayBuilder;
import java.io.*;
import java.net.*;
import java.util.zip.GZIPInputStream;

/**
 *
 * @author callum
 */
public class DataTransferIn implements Runnable{
    Socket inSoc, outSoc;
    boolean gzip;
    ArrayBuilder arr;

    public DataTransferIn(Socket in, Socket out){
        inSoc = in;
        outSoc = out;
    }
    
    @Override
    public void run() {
        int n = 0;
        byte[] buffer = new byte[65536];
        try {
            InputStream inS = inSoc.getInputStream();
            OutputStream outS = null;
            if (outSoc != null) {
                outS = outSoc.getOutputStream();
            }
            arr = new ArrayBuilder();
            while((n = inS.read(buffer, 0, buffer.length)) != -1)
            {
                arr.append(buffer, n);
                if (outSoc != null) {                
                    try {
                        outS.write(buffer, 0, n);
                        outS.flush();
                    } catch (Exception e) {}
                }
            }
            forward();
            inSoc.close();
        } catch (Exception e) {} 
    }
    
    private void forward() {     
        while (arr != null && arr.getSize() > 0) {
            int n = -1;
            gzip = false;
            for (int i = 0; i < arr.endHeader(); i++) {
                if (arr.getLine(i).startsWith((char)10 + "Content-Length: ")) {
                    n = new Integer(arr.getLine(i).substring(17, arr.getLine(i).length() - 1));
                }
                if (arr.getLine(i).startsWith((char)10 + "Content-Encoding: gzip")) {
                    gzip = true;
                }
            }

//            System.out.println(n);

//            System.out.print(arr.getLine(arr.endHeader()));//blank
//            System.out.print(new String(arr.getArr(), arr.dataStart(), n));
            if (gzip) {
                try {
                    InputStream is = new ByteArrayInputStream(arr.getArr(), arr.dataStart(), n);
                    GZIPInputStream gzin = new GZIPInputStream(is);
                    StringBuilder sb = new StringBuilder();
                    String gzipResponse;
                    byte[] buffer = new byte[65536];
                    int response;
                    while((response = gzin.read(buffer, 0, buffer.length)) > 0){
                        gzipResponse = new String(buffer, 0, response);
                        sb.append(gzipResponse);
                    }
//                    System.out.print(sb.toString());
                    GateWarsFactory.parse(sb.toString());
                    gzin.close();
                } catch (Exception e) {
//                    e.printStackTrace();
                }
            }

            if (n > 0) {
                ArrayBuilder temp = new ArrayBuilder();
                temp.append(arr.getArr(), arr.dataStart() + n, arr.getSize() - (arr.dataStart() + n));
                arr = temp;
            } else {
                arr = null;
            }
        }  
    }
} 